package com.jackrain.nea.omslocal.task;/**
 * Created by Administrator on 2017/3/12.
 */


import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.omslocal.service.JDQuantityQueryService;
import com.jackrain.nea.omslocal.service.QuantityQueryService;
import com.jackrain.nea.omslocal.service.VipQuantityQueryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * Description: 自动任务,查询库存表,发送同步库存消息
 * Created with IntelliJ IDEA.
 * User: cs
 * Date: 2017/4/13
 * Time: 12:44
 */
@Component
@Slf4j
public class AutoUpdateQty {

    @Autowired
    private QuantityQueryService qtqs;
    @Autowired
    private PropertiesConf pconf;
    @Autowired
    private JDQuantityQueryService jdqtqs;
    @Autowired
    private VipQuantityQueryService vipqtqs;

    public void getScheduledExecutorA() {

        log.info("AutoMotThreads UpdateQTY started !!!");
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                try {
                     qtqs.queryQty();
                } catch (Exception e) {
                    e.printStackTrace();
                    // 执行任务错误,将日志记录
                }
            }
        };
        ScheduledExecutorService service = new ScheduledThreadPoolExecutor(3,
                new BasicThreadFactory.Builder().namingPattern("autotaskqty-pool-%d").daemon(true).build());
        // 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间
        // 正式 要延迟适当时间,以便时间初始化赋值
        service.scheduleAtFixedRate(runnable, 2,pconf.getPropertyInt("ons.qtytime") ,TimeUnit.SECONDS);
    }

    public void getScheduledExecutorB() {

        log.info("AutoMotThreads qty started !!!");
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    qtqs.qtyJdStart();
                } catch (Exception e) {
                    e.printStackTrace();
                    // 执行任务错误,将日志记录
                }
            }
        };

        ScheduledExecutorService service = Executors
                .newSingleThreadScheduledExecutor();
        // 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间
        // 正式 要延迟适当时间,以便时间初始化赋值
        service.scheduleAtFixedRate(runnable, 20,pconf.getPropertyInt("ons.qtytime") ,TimeUnit.SECONDS);
    }

    /**
     * 京东库存同步任务
     */
    public void getScheduledExecutorC() {

        log.info("AutoMotThreads jdqty started !!!");
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    jdqtqs.queryQty();
                } catch (Exception e) {
                    e.printStackTrace();
                    // 执行任务错误,将日志记录
                }
            }
        };

        ScheduledExecutorService service = new ScheduledThreadPoolExecutor(3,
                new BasicThreadFactory.Builder().namingPattern("autotaskjdqty-pool-%d").daemon(true).build());
        // 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间
        // 正式 要延迟适当时间,以便时间初始化赋值
        service.scheduleAtFixedRate(runnable, 20,pconf.getPropertyInt("ons.qtytime") ,TimeUnit.SECONDS);
    }

    /**
     *OXO增量库存同步与共享库存增量同步
     */
    public void getScheduledExecutorD() {
        log.info("AutoMotThreads VipQTY started !!!");
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    vipqtqs.queryQty();
                } catch (Exception e) {
                    e.printStackTrace();
                    // 执行任务错误,将日志记录
                }
            }
        };
        ScheduledExecutorService service = new ScheduledThreadPoolExecutor(3,
                new BasicThreadFactory.Builder().namingPattern("autotaskvipqty-pool-%d").daemon(true).build());
        // 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间
        // 正式 要延迟适当时间,以便时间初始化赋值
        service.scheduleAtFixedRate(runnable, 30,pconf.getPropertyInt("ons.vipqtytime") ,TimeUnit.SECONDS);
    }


}
